<template>
  <div class="add-card">
    <header class="add-header">
      <el-page-header content="续费月卡" @back="$router.back()" />
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">车辆信息</div>
        <div class="form">
          <el-form ref="form" label-width="100px" :model="cardData" :disabled="true">
            <el-form-item label="车主姓名">
              <el-input v-model="cardData.personName" />
            </el-form-item>
            <el-form-item label="联系方式">
              <el-input v-model="cardData.phoneNumber" />
            </el-form-item>
            <el-form-item label="车辆号码">
              <el-input v-model="cardData.carNumber" />
            </el-form-item>
            <el-form-item label="车辆品牌">
              <el-input v-model="cardData.carBrand" />
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="form-container">
        <div class="title">最新一次月卡缴费信息</div>
        <div class="form">
          <el-form ref="dateForm" label-width="100px" :model="cardData" :rules="dateRules">
            <el-form-item label="有效日期" prop="cardStartDate">
              <el-date-picker
                v-model="cardData.cardStartDate"
                type="date"
                placeholder="开始日期"
                :disabled="true"
                style="margin-right: 10px; width: 48%;"
              />
              <el-date-picker
                v-model="cardData.cardEndDate"
                type="date"
                placeholder="结束日期"
                style="width: 48%;"
              />
            </el-form-item>
            <el-form-item label="支付金额" prop="paymentAmount">
              <el-input v-model="cardData.paymentAmount" />
            </el-form-item>
            <el-form-item label="支付方式" prop="paymentMethod">
              <el-select v-model="cardData.paymentMethod" placeholder="请选择">
                <el-option
                  v-for="item in cardData.payment"
                  :key="item.industryCode"
                  :value="item.industryCode"
                  :label="item.industryName"
                />
              </el-select>
            </el-form-item>
          </el-form>
        </div>
      </div>

    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button @click="reset">重置</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </footer>
  </div>
</template>
<script>
import { renewCardAPI, getEditCardAPI } from '@/api/addCard'
export default {
  data() {
    return {
      // 表单数据
      cardData: {
        personName: '',
        phoneNumber: '',
        carNumber: '',
        carBrand: '',
        cardStartDate: '',
        cardEndDate: '',
        paymentAmount: '',
        paymentMethod: null,
        carInfoId: null,
        rechargeId: null,
        // 支付方式
        payment: [

          {
            industryCode: 'Alipay',
            industryName: '支付宝'
          },
          {
            industryCode: 'WeChat',
            industryName: '微信'
          },
          {
            industryCode: 'Cash',
            industryName: '现金'
          }
        ]

      },
      // 日期校验规则
      dateRules: {
        cardStartDate: [{ required: true, message: '请选择有效日期', trigger: 'blur' }],
        paymentAmount: [
          { required: true, message: '请输入支付金额', trigger: 'blur' }
        ],
        paymentMethod: [
          { required: true, message: '请选择支付方式', trigger: 'change' }]
      }
    }
  },
  computed: {
    // 获取路由参数的id
    id() {
      return this.$route.query.id
    }
  },
  async mounted() {
    await this.getEditCard(this.id)
  },
  methods: {
    // 查看月卡详情
    async  getEditCard(id) {
      const res = await getEditCardAPI(id)
      this.cardData = {
        ...res.data,
        payment: [...this.cardData.payment]
      }
      this.cardData.carInfoId = res.data.carInfoId
    },
    // 提交续费信息
    async renewCard() {
      const { cardStartDate, cardEndDate, paymentAmount, paymentMethod, carInfoId } = this.cardData
      await renewCardAPI({ cardStartDate, cardEndDate, paymentAmount, paymentMethod, carInfoId })
    },
    // 提交
    async submit() {
      await this.renewCard()
      this.$message.success('续费成功')
      this.$router.back()
    },
    // 重置
    reset() {}

  }
}

</script>
<style scoped lang="scss">
.add-card {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    align-items: center;
    padding: 0 20px;
    height: 64px;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }
      .arrow{
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    background: #f4f6f8;
    padding: 20px 130px;

    .form-container {
      background-color: #fff;

      .title {
        height: 60px;
        line-height: 60px;
        padding-left: 20px;
      }

      .form {
        margin-bottom: 20px;
        padding: 20px 65px 24px;

        .el-form {
          display: flex;
          flex-wrap: wrap;

          .el-form-item {
            width: 50%;
          }
        }
      }
    }
    .preview{
      img{
        width: 100px;
      }
    }
  }

  .add-footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 24px 50px;
    color: #000000d9;
    font-size: 14px;
    background: #fff;
    text-align: center;
  }
}
</style>
